package com.dfiov.cloud.msmtools.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;

/**
 * Jwt工具类
 */
public class JwtUtil {

    //自定密钥
    public static final String tokenKey = "dftc20250307";

    /*
    生成票证
     */
    public static String getSign(HashMap<String,Object> headMap,HashMap<String,String> claimMap,int days){
        LocalDateTime localDateTime = LocalDateTime.now().plus(days, ChronoUnit.DAYS);
        Date date = LocalDateTimeUtil.localDateTimeToDate(localDateTime);
        JWTCreator.Builder builder = JWT.create().withHeader(headMap);
        claimMap.forEach(builder::withClaim);
        builder.withExpiresAt(date);
        return builder.sign(Algorithm.HMAC256(tokenKey));
    }


    /*
    获取token信息，token不对会异常
     */
    public static DecodedJWT verify(String token){
        return JWT.require(Algorithm.HMAC256(tokenKey)).build().verify(token);
    }
}